package com.xd.study.activity;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xd.study.R;

/**
 * Created by DGF on 2015/4/30.
 */
public class NotificationActivity extends Activity {

    NotificationManager manager;
    private int notificationID = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notification);

        Button btnSend = (Button)this.findViewById(R.id.notification_btn_send);
        Button btnCancel = (Button)this.findViewById(R.id.notification_btn_cancel);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 构造notification并发送到通知栏
                 */
                Notification.Builder builder = new Notification.Builder(NotificationActivity.this);
                builder.setSmallIcon(R.drawable.item);//通知的图标
                builder.setTicker("Hello");//手机状态栏的提示
                builder.setWhen(System.currentTimeMillis());//设置时间
                builder.setContentTitle("通知栏通知");//设置标题
                builder.setContentText("我来自NotificationDemo");//设置通知内容

                PendingIntent pendingIntent = PendingIntent.getActivity(
                        NotificationActivity.this,
                        0,
                        new Intent(NotificationActivity.this, NotificationActivity.class),
                        0);
                builder.setContentIntent(pendingIntent);//设置点击后的意图

                //设置默认效果（需要设置权限）
                builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
                builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
                builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
//                builder.setDefaults(Notification.DEFAULT_ALL);//包括声音、指示灯、震动

                //Notification notification = builder.build();//4.1及以上使用该方法
                Notification notification = builder.getNotification();//4.1以下

                manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                manager.notify(notificationID, notification);
                Toast.makeText(NotificationActivity.this, "NotificationID = "+notificationID, Toast.LENGTH_SHORT).show();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(NotificationActivity.this, "manager = "+manager, Toast.LENGTH_SHORT).show();
                Toast.makeText(NotificationActivity.this, "NotificationID = "+notificationID, Toast.LENGTH_SHORT).show();
                manager.cancel(notificationID);
            }
        });
    }
}
